package com.example.mobile2121606110syc0407;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button btnMainSupplier;
    private Button btnMainBuy;
    private TextView tvMainResult;
    private Button btnMainClose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog dialog;
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("对话框");
                builder.setMessage("确定退出应用");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        MainActivity.this.finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                dialog=builder.create();
                dialog.show();
            }
        });
        btnMainSupplier.setOnClickListener(new View.OnClickListener() {
            String[] supplies={"新星汽车配件","枫叶汽车配件","天辉汽车配件","西末汽车配件"};
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请选择供应商")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        })
                        .setSingleChoiceItems(supplies, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                tvMainResult.setText(supplies[i]);

                            }
                        })
                        .create()
                        .show();
            }
        });
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            String[] parts = new String[]{"轿车引擎","轿车前玻璃","轿车方向盘"};
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请选择原材料")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                tvMainResult.setText(null);
                                ListView listView=((AlertDialog)dialogInterface).getListView();
                                for (int j=0;j<parts.length;j++){
                                    if(listView.getCheckedItemPositions().get(j)){
                                        tvMainResult.append(parts[j]+"\n");
                                    }
                                }
                                dialogInterface.dismiss();
                            }
                        })
                        .setMultiChoiceItems(parts, null, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i, boolean b) {

                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    private void initView() {
        btnMainSupplier = (Button) findViewById(R.id.btnMainSupplier);
        btnMainBuy = (Button) findViewById(R.id.btnMainBuy);
        tvMainResult = (TextView) findViewById(R.id.tvMainResult);
        btnMainClose = (Button) findViewById(R.id.btnMainClose);
    }
}